package com.sskj.mine.asset;

import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;
import com.sskj.mine.asset.data.BillBean;
import com.sskj.mine.asset.data.CoinTypeBean;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.presenter.BasePresenter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import okhttp3.Response;


/**
 * @author Hey
 * Create at  2019/05/04
 */
class BillPresenter extends BasePresenter<BillActivity> {


    private Disposable typeDispo;

    public void getType() {
        Flowable<List<CoinTypeBean>> assetFlow = httpService.getAssetType()
                .converter(new JsonConverter<HttpData<List<CoinTypeBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(listHttpData -> listHttpData.getData());
        Flowable<Map<String, String>> recordFlow = httpService.getRecordType()
                .converter(new JsonConverter<HttpData<Map<String, String>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(mapHttpData -> mapHttpData.getData());
        typeDispo = Flowable.zip(assetFlow, recordFlow, (BiFunction<List<CoinTypeBean>, Map<String, String>, Object>) (assetTypeBeans, recordStringMap) -> {
            mView.updateType(assetTypeBeans, recordStringMap);
            return "1";
        }).subscribe(o -> {
        }, e -> {
        });
    }

    /**
     * 资产流水 账单
     *
     * @param wall_type \t财务类型
     * @param pid       币种id
     * @return
     */
    public Flowable<List<BillBean>> getAssetRecordAll(String p, String size, String wall_type, String pid) {
        return httpService.getAssetRecordAll(p, size, wall_type, pid).
                converter(new JsonConverter<HttpData<PageBean<BillBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList())
                .onErrorReturnItem(new ArrayList<>());
    }

    @Override
    public void cancelRequest() {
        DisposUtil.close(typeDispo);
        super.cancelRequest();
    }
}
